package com.guanghua.brick.html;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guanghua.brick.util.StringUtil;

public class SelectControl extends WebControl {
	
	//private static Log logger = LogFactory.getLog(SelectControl.class);
	
	private String textProperty = "text";
	private String valueProperty = "value";
	private String url = null;
	private String method = "POST";
	private boolean lazy = false;
	
	private boolean multiple = false;
	private boolean emptyOption = false;



	@Override
	public String buildHTML(HttpServletRequest request,  HttpServletResponse response) {
		StringBuffer html = new StringBuffer();
		html.append("<select id=\"")
			.append(this.getId())
			.append("\" name=\"")
			.append(this.getName())
			.append("\" ");
		
		//如果有url则通过url加载content
		if (this.url != null)
			html.append("url=\"")
				.append(this.url)
				.append("\" ");
		//传入content id
		if (this.getContent() != null)
			html.append("content=\"")
			.append(this.getContent().getId())
			.append("\" ");
		if (this.multiple)
			html.append("multiple ");
		
		html.append("method=\"")
			.append(this.method)
			.append("\" valueProperty=\"")
			.append(this.valueProperty)
			.append("\" textProperty=\"")
			.append(this.textProperty)
			.append("\" ")
			.append(this.getHtml())
			.append(">\n");
		
		//有content且lazy为false说明不是延迟加载
		if (this.getContent() != null && !this.lazy) {
			List<Map<String, String>> list = this.getContent().buildContent(request, response);
			
			if (this.emptyOption) {
				Map<String, String> map = new HashMap<String, String>();
				map.put(this.textProperty, "");
				map.put(this.valueProperty, "");
				list.add(0, map);
			}
			
			Iterator<Map<String, String>> i = list.iterator();
			while (i.hasNext()) {
				Map<String, String> map = i.next();
				String text = map.get(this.textProperty);
				String value = map.get(this.valueProperty);
				
				if (text == null || value == null) continue;
								
				if (isSelected(value)) {
					html.append("<option value=\"")
					.append(value)
					.append("\" selected>")
					.append(text)
					.append("</option>\n");
				} else {
					html
					.append("<option value=\"")
					.append(value)
					.append("\">")
					.append(text)
					.append("</option>\n");
				}
			}
		}
		
		html.append("</select>\n");
		
		//scripts生成
		html.append("<script language=\"javascript\">\n");
		html.append("	var ")
			.append(this.getId())
			.append(" = new Select(\"")
			.append(this.getId())
			.append("\"");
		
		String d = StringUtil.stringArrayToString(this.getDatas());
		if (d != null && d.length() != 0)
			html.append(", \"")
				.append(d)
				.append("\"");
		
		html.append(");\n");
		
		//生成depend
		if (this.getDepends() != null) {
			Iterator<IDepend> i = this.getDepends().iterator();
			while (i.hasNext()) {
				html.append("	")
				.append(i.next().buildScripts(this.getId()))
				.append("\n");
			}
		}
		
		html.append("</script>");
		
		return html.toString();
	}

	public String getMethod() {
		return method;
	}


	public void setMethod(String method) {
		this.method = method;
	}


	public String getTextProperty() {
		return textProperty;
	}


	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public String getValueProperty() {
		return valueProperty;
	}


	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}
	
	
	public boolean isLazy() {
		return this.lazy;
	}
	
	
	public void setLazy(boolean lazy) {
		this.lazy = lazy;
	}
	

	public boolean isMultiple() {
		return multiple;
	}

	public void setMultiple(boolean multiple) {
		this.multiple = multiple;
	}
	
	public boolean isEmptyOption() {
		return emptyOption;
	}

	public void setEmptyOption(boolean emptyOption) {
		this.emptyOption = emptyOption;
	}
	
}
